import re

from model.models import Point, User
from server.utils import Code


class Verify:
    """ Verify the legitimacy of request parameters """

    @classmethod
    def username(cls, username):
        """ Verify the format of the username """
        if type(username) is not str:
            return -1, 'The username must be of character type'
        if not re.fullmatch(r'\w{6,20}', username):
            return -1, 'The username is limited to 6 to 20 digits in length and can only contain letters, numbers, and underscores'
        return Code.OK

    @classmethod
    def password(cls, password):
        """ Verify the format of the password """
        if type(password) is not str:
            return -1, 'The password must be of character type'
        if not re.fullmatch(r'\w{6,20}', password):
            return -1, 'The password is limited to 6 to 20 digits in length and can only contain letters, numbers, and underscores'
        return Code.OK

    @classmethod
    def phone(cls, phone):
        """ Verify the format of the phone """
        # if type(phone) is not str:
        #     return -1, 'The phone must be of character type'
        # if not re.fullmatch(r'\w{6,20}', phone):
        #     return -1, 'The phone is limited to 6 to 20 digits in length and can only contain letters, numbers, and underscores'
        return Code.OK

    @classmethod
    def longitude(cls, longitude):
        """ Verify the format of the longitude """
        if type(longitude) not in [int, float]:
            return -1, 'The longitude must be of type int or float'
        if not -180 <= longitude <= 180:
            return -1, 'The longitude range must be between [-180, 180]'
        return Code.OK

    @classmethod
    def latitude(cls, latitude):
        """ Verify the format of the longitude """
        if type(latitude) not in [int, float]:
            return -1, 'The latitude must be of character type'
        if not -90 <= latitude <= 90:
            return -1, 'The latitude range must be between [-90, 90]'
        return Code.OK

    @classmethod
    def direction(cls, direction):
        """ Verify the format of the direction """
        if type(direction) not in [str]:
            return -1, 'The direction must be of character type'
        if not 0 <= len(direction) <= 200:
            return -1, 'The direction is limited to 0 to 200 digits in length'
        return Code.OK

    @classmethod
    def pointPid(cls, pid):
        """ Verify the format of the point pid """
        try:
            Point.objects.get(pid=pid)
            return Code.OK
        except Exception as e:
            return -1, 'Unable to find the point information'

    @classmethod
    def userUid(cls, uid):
        """ Verify the format of the point uid """
        try:
            User.objects.get(uid=uid)
            return Code.OK
        except Exception as e:
            return -1, 'Unable to find the user information'

    @classmethod
    def pointName(cls, name):
        """ Verify the format of the point name """
        if type(name) is not str:
            return -1, 'The point name must be of character type'
        if not re.fullmatch(r'[\w ]{1,20}', name):
            return -1, 'The point name is limited to 1 to 20 digits in length and can only contain letters, numbers, and underscores'
        return Code.OK

    @classmethod
    def pointDetail(cls, detail):
        """ Verify the format of the point detail """
        if type(detail) is not str:
            return -1, 'The point detail must be of character type'
        if not re.fullmatch(r'[\w ]{1,200}', detail):
            return -1, 'The point detail is limited to 1 to 200 digits in length and can only contain letters, numbers, and underscores'
        return Code.OK

    @classmethod
    def pointPhoto(cls, photo):
        """ Verify the format of the point photo """
        if not photo:
            return -1, 'No photo uploaded'
        if photo.content_type not in ['image/png', 'image/jpg', 'image/jpeg', 'image/bmp']:
            return -1, 'The photo format must be [bmp, jpg, jpeg, png]'
        return Code.OK

    @classmethod
    def pointIcon(cls, photo):
        """ Verify the format of the point icon """
        return cls.pointPhoto(photo)
